#include "stdafx.h"
namespace comms{

//-----------------------------------------------------------------------------
// cSurface::Map.ctor
//-----------------------------------------------------------------------------
cSurface::Map::Map() {
	Unit = 0;
	MipMap = true;
	//id = cRender::idNone;
	Offset.SetZero();
	Scale.SetOne();
} // cSurface::Map.ctor

//-----------------------------------------------------------------------------
// cSurface::Map::Load
//-----------------------------------------------------------------------------
bool cSurface::Map::Load() {
	//id = cRender::idNone;
	if(FileName.Length() == 0) {
		return true;
	}
	
	//id = cRender::GetInstance()->AddTexture(FileName, MipMap);
	return false;//id != cRender::idNone;
} // cSurface::Map::Load

// cSurfSObjectace.ctor
cSurface::cSurface() {
    UseLimitAngle=false;
    LimitAngle=3.1415/6;
    Luminosity=0;
	Reflection=0;
    Refraction=0;
    Transparency=0;
    Translucensy=0;
	Shininess=0;
    cColor id(255,255,255,255);
    cColor z(0,0,0,0);
    DepthModulator=1;
    memcpy(&Color,&id,sizeof(id));
    memcpy(&Ambient,&z,sizeof(id));
    memcpy(&Diffuse,&id,sizeof(id));
    memcpy(&Specular,&z,sizeof(id));
    memcpy(&Gloss,&id,sizeof(id));
}

//---------------------------------------------------------------------------------------------------------------------------------
// cSurface.ctor
//---------------------------------------------------------------------------------------------------------------------------------
cSurface::cSurface(const char *Name, const cColor &Ambient, const cColor &Diffuse, const cColor &Specular, const float Shininess)
: Name(Name), Ambient(Ambient), Diffuse(Diffuse), Specular(Specular), Shininess(Shininess) {
	for(int i = 0; i < 8; i++) {
		Tex[i].Unit = i;
	}
    UseLimitAngle=false;
    LimitAngle=3.1415/6;
    DepthModulator=1;
    Luminosity=0;
    Reflection=0;
    Refraction=0;
    Transparency=0;
    Translucensy=0;
    DepthModulator=1;
    Color=cColor(1);	
    Gloss=cColor(0);
} // cSurface.ctor

//-----------------------------------------------------------------------------
// cSurface::Load
//-----------------------------------------------------------------------------
void cSurface::Load() {
	for(int i = 0; i < 8; i++) {
		Tex[i].Load();
	}
} // cSurface::Load

void cSurface::CopyTo(cSurface& dest){
    dest.Name=Name;
    dest.Color=Color;
	dest.Ambient=Ambient;
	dest.Diffuse=Diffuse;
	dest.Specular=Specular;
    dest.Gloss=Gloss;
    dest.Luminosity=Luminosity;
    dest.Reflection=Reflection;
    dest.Refraction=Refraction;
    dest.Transparency=Transparency;
    dest.Translucensy=Translucensy;
	dest.Shininess=Shininess;
    dest.DepthModulator=DepthModulator;
    dest.UseLimitAngle=UseLimitAngle;
    dest.LimitAngle=LimitAngle;
    for(int i=0;i<ExData.Count();i++){
        cMtlDataChunk c;
        dest.ExData.Add(c);
        cMtlDataChunk& dc=dest.ExData.GetLast();
        dc.Type=ExData[i].Type;
        int sz=ExData[i].Data.Count();
        dest.ExData[i].Data.Add(0,sz);
        memcpy(dest.ExData[i].Data.ToPtr(),ExData[i].Data.ToPtr(),sz);
    }
}
void WriteColor(cColor& c,comms::cFile& d){
    d.WriteFloat(c.r);
    d.WriteFloat(c.g);
    d.WriteFloat(c.b);
    d.WriteFloat(c.a);
}
void ReadColor(cColor& c,comms::cFile& d){
    c.r=d.ReadFloat();
    c.r=d.ReadFloat();
    c.r=d.ReadFloat();
    c.r=d.ReadFloat();
}
void cSurface::Store(comms::cFile& dest){
    dest.WriteDword(ExData.Count());
    for(int i=0;i<ExData.Count();i++){
        dest.WriteDword(ExData[i].Type);
        int sz=ExData[i].Data.Count();
        dest.WriteDword(sz);
        dest.Write(ExData[i].Data.ToPtr(),sz);
    }

    dest.WriteDword(1);
    WriteColor(Color,dest);
    WriteColor(Ambient,dest);
    WriteColor(Diffuse,dest);
    WriteColor(Specular,dest);
    WriteColor(Gloss,dest);
    dest.WriteFloat(Luminosity);
    dest.WriteFloat(Reflection);
    dest.WriteFloat(Refraction);
    dest.WriteFloat(Transparency);
    dest.WriteFloat(Translucensy);
    dest.WriteFloat(Shininess);
    dest.WriteFloat(DepthModulator);
    dest.WriteByte(UseLimitAngle);
    dest.WriteFloat(LimitAngle);       
}
void cSurface::ReStore(comms::cFile& dest){
    int n=dest.ReadDword();
    for(int i=0;i<n;i++){
        cMtlDataChunk c;
        ExData.Add(c);
        cMtlDataChunk& dc=ExData.GetLast();
        dc.Type=dest.ReadDword();
        int sz=dest.ReadDword();
        dc.Data.Add(0,sz);
        dest.Read(dc.Data.ToPtr(),sz);
    }
    DWORD v=dest.ReadDword();
    if(v==1){
        ReadColor(Color,dest);
        ReadColor(Ambient,dest);
        ReadColor(Diffuse,dest);
        ReadColor(Specular,dest);
        ReadColor(Gloss,dest);
        Luminosity=dest.ReadFloat();
        Reflection=dest.ReadFloat();
        Refraction=dest.ReadFloat();
        Transparency=dest.ReadFloat();
        Translucensy=dest.ReadFloat();
        Shininess=dest.ReadFloat();
        DepthModulator=dest.ReadFloat();
        UseLimitAngle=dest.ReadByte();
        LimitAngle=dest.ReadFloat();
    }
}
}